package com.ys.controller.api.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ys.entity.base.TbBaseInfoArea;
import com.ys.service.sys.CommonApiService;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;

/**
 * 省市县信息表
 */
@Controller
@RequestMapping("/api/inter/tbBaseInfoArea")
public class ApiInterTbBaseInfoAreaController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTbBaseInfoAreaController.class);

    @Autowired
    private CommonApiService<TbBaseInfoArea> commonApiService;



    /**
     * 弹出框信息查询-区县信息
     */
    @RequestMapping("/searchForModalByCountry")
    @ResponseBody
    public LayuiTableVoJA searchForModalByCountry(@ModelAttribute TbBaseInfoArea cond,String conditions){
        try {
            //查询条件
            String province = ConditionUtil.getConditionValue(conditions, "province");//省份
            String city = ConditionUtil.getConditionValue(conditions, "city");//市
            String county  = ConditionUtil.getConditionValue(conditions, "county");//区县
            String whereString = "A.Level=3 ";//只查询区县信息
            if(province!=null && !"".equals(province)){
                whereString += " AND A.HigherLevelPath Like '%"+province+"%' ";
            }
            if(city!=null && !"".equals(city)){
                whereString += " AND A.HigherLevelPath Like '%"+city+"%' ";
            }
            if(county!=null && !"".equals(county)){
                whereString += " AND A.Name Like '%"+county+"%' ";
            }
            int page = cond._ys_page;
            int size = cond._ys_size;
            TbBaseInfoArea condNew = new  TbBaseInfoArea();
            condNew.setPage(page);
            condNew.setSize(size);
            condNew.addField("A.Name AS county");
            condNew.addField("A.HigherLevelPath + ',' + A.Name AS detailPath");
            condNew.setOrderby("A.Code ASC");
            condNew.where(whereString);

            LayuiTableVoJA tableVoJA = commonApiService.searchLay(condNew);
            //获取
            JSONArray tableVoJAData = tableVoJA.getData();

            //转换
            List<Map> resList = JSONArray.parseArray(JSON.toJSONString(tableVoJAData),Map.class);
            List<Map<String, Object>> resListNew = new ArrayList();
            resList.forEach(item-> resListNew.add(item));

            //处理数据
            resList = new ArrayList();
            for(Map<String, Object> item:resListNew){
                String higherLevelPath = item.get("higherLevelPath").toString();
                String[] path = higherLevelPath.split(",");
                item.put("province",path[0]);//获取省份
                item.put("city",path[1]);//获取市
                resList.add(item);
            }
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(resList));
            tableVoJA.setData(jsonArray);

            return tableVoJA;
        }catch (Exception e){
            logger.error("弹出框信息查询-区县信息*出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }


}